<?php

namespace Member\Model;

use Think\Model;

/**
 * 用户基本信息类
 *
 * @author Jeff
 */
class MemberModel extends Model {

    /**
     * 获取用户的基本信息
     * 从缓存中取出，如果没有再从数据库中查询，再写入缓存
     * @param $username 用户名/手机或UID
     * @param $type 区别是根据用户名获取或UID获取
     * @param $type=1 用户名/手机，$type=2 UID获取
     * @param $appid 应用ID，默认为skinrun
     */
    function getUserInfo($username = '' , $type = 1) {
        
        $config = C('ALLOW_OTHER_APP_LOGIN');
        $appid = APPID;
        
        $userinfo = array();
        
        if($type == 2) {
            $key = get_cache_key($username , $type);
            $userinfo = S($key);
        }
        //缓存中不存在
        if(!$userinfo) {
            //用户名或手机
            if($type == 1) {
                $where['a.mobile'] = $username;
                
                if(!$config)
                    $where['b.appid']  = $appid;
                
                $where['a.status'] = 1;
                
                $uinfo = $this->alias('a')->field('a.*')->join('hjy_member_account as b on a.id = b.rootid' , 'LEFT')->where($where)->find();
                
                unset($where);
                
                if($uinfo) {
                    $userinfo['member'] = $uinfo;
                    //如果用户信息存在
                    $where['rootid'] = $uinfo['id'];
                    $where['type']   = 0;
                    $userinfo['account'] = M('member_account')->where($where)->find();
                    
                    unset($where);
                    $where['uid']    = $userinfo['account']['uid'];
                    $userinfo['profile'] = M('member_profile')->where($where)->find();
                    
					if($userinfo['profile']['nickname']) {
						$userinfo['profile']['full_nickname'] = $userinfo['profile']['nickname'];
						$userinfo['profile']['nickname'] = msubstr($userinfo['profile']['nickname'] , 14);
						
					}
                    //获取用户积分,不记录缓存
                    //$credit = M('member_credits')->where($where)->find();
                    //获取用户所在地区
                    if($userinfo['profile']['regions']) {
                        $regions = D('Member/CommonRegions');
                        $userinfo['regions'] = $regions->getDistrict($userinfo['profile']['regions']);
                    }
                }
            } else {
                //uid获取
                //如果用户信息存在
                $where['uid'] = $username;
                $uinfo = M('member_account')->where($where)->find();
                if($uinfo) {
                    $userinfo['account'] = $uinfo;
                    $userinfo['profile'] = M('member_profile')->where($where)->find();

					if($userinfo['profile']['nickname']) {
						$userinfo['profile']['full_nickname'] = $userinfo['profile']['nickname'];
						$userinfo['profile']['nickname'] = msubstr($userinfo['profile']['nickname'] , 14);
						$userinfo['profile']['groupid'] = $userinfo['profile']['groupid'];
						
					}

                    unset($where);
                    $where['id'] = $uinfo['rootid'];
                    $userinfo['member'] = $this->where($where)->find();
                    unset($where);
                    $where['uid'] = $username;
                    //获取用户积分,不记录缓存
                    //$credit = M('member_credits')->where($where)->find();
                    //获取用户所在地区
                    if($userinfo['profile']['regions']) {
                        $regions = D('Member/CommonRegions');
                        $userinfo['regions'] = $regions->getDistrict($userinfo['profile']['regions']);
                    }
                }
            }
            
            if($userinfo) {
                //写入缓存
                //手机为键值
//                 S(get_cache_key($userinfo['member']['mobile'] , 1) , $userinfo);
                
                //UID为键值
                S(get_cache_key($userinfo['account']['uid'] , 2) , $userinfo);
            }
        } else {
            //获取用户积分
			//$credit = M('member_credits')->where(array('uid' => $userinfo['account']['uid']))->find();
        }
		//dump($userinfo);exit;
        $credit = M('MemberCredits')->field('credit2,credit1')->where( array('uid'=>$userinfo['account']['uid']) )->find();
        $userinfo['profile']['credit1']  = $credit['credit1'];
        $userinfo['profile']['credit2']  = $credit['credit2'];
        
		if( $userinfo['profile']['groupid'] == 1 ){
			if( $credit['credit2'] >= C('MASTER_CREDIT') ){
				$userinfo['profile']['groupid'] = 99;
			}
		}
        //$userinfo['credit'] = $credit;
        //返回用户信息
        return $userinfo;
    }
    
    
}
